﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.Web.UI.WebControls;
using Ornament.MemberShip;
using Ornament.MemberShip.Module.Presenters;
using Ornament.MemberShip.Module.Views;

public partial class Admin_MemberAdmin_Controls_RoleEditorView : UserControl, IRoleEditorView
{
    
    #region IRoleEditorView Members

    public RoleManagerPresenter Presenter { get; set; }

    public int PageSize
    {
        get { return ListView1.PageSize; }
    }


    public void BindRoleList(IList<Role> role, int totalCount)
    {
        ObjectContainerDataSource1.DataSource = role;
        ObjectContainerDataSource1.DataBind();
    }

    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        Presenter = new RoleManagerPresenter(this);
        if (!IsPostBack)
        {
            Presenter.InitilizeView();
        }
        Presenter.ViewLoad();
    }

    protected void ObjectContainerDataSource1_Updated(object sender, ObjectContainerDataSourceStatusEventArgs e)
    {
        Presenter.Update((Role) e.Instance);
    }

    protected void ObjectContainerDataSource1_Selecting(object sender, ObjectContainerDataSourceSelectingEventArgs e)
    {
        Presenter.BindRoleList(e.Arguments.StartRowIndex);
    }

    protected void ObjectContainerDataSource1_Creating(object sender, ObjectContainerDataSourceCreatingObjectArgs e)
    {
        switch (e.CurrentOperation)
        {
            case ObjectContainerOperator.Insert:
                e.NewObjectInstance = new Role();
                break;
            default:
                {
                    string id = null;
                    foreach (string ids in e.Keys.Values)
                    {
                        id = ids;
                    }
                    e.NewObjectInstance = Presenter.GetRole(id);
                    break;
                }
        }
    }


    protected void NameDuplicateChecking(object source, ServerValidateEventArgs args)
    {
        args.IsValid = !Presenter.ExistRole(args.Value);
    }

    protected void ObjectContainerDataSource1_Deleted(object sender, ObjectContainerDataSourceStatusEventArgs e)
    {
        Presenter.Delete((Role) e.Instance);
    }

    protected void ObjectContainerDataSource1_Inserted(object sender, ObjectContainerDataSourceStatusEventArgs e)
    {
        Presenter.Update((Role) e.Instance);
    }

    protected void addNewRole(object sender, EventArgs e)
    {
        FormView1.ChangeMode(FormViewMode.Insert);
    }
}